<?php
error_reporting(0);

$cata_name = $_POST['newcata'];
session_start();

if(!isset($_SESSION['username'])){
    // echo '<script>alert("未登录");location="./login.html"</script>';
    exit(0);
}
$cata_user = $_SESSION['username'];
if ($cata_name == '') {
    // echo '<script>alert("不能为空");history.go(-1);</script>';
    exit(0);
}
if (strlen($cata_name) >256) {
    // echo '<script>alert("内容过长");history.go(-1);</script>';
    exit(0);
}

include('./connect.php');
if ($conn->connect_error) {
    // echo '数据库连接失败！';
    exit(0);
} else {
    
    $sql_insert = "insert into cata (cata_name,cata_user) values('$cata_name','$cata_user')";
    $res_insert = $conn->query($sql_insert);

        
    //立即查询数据库，拿到id
    $sql_cata_select = "select cata_id from cata where cata_user = '$cata_user' order by cata_id desc";
    $result = mysqli_query($conn,$sql_cata_select);
    $number = mysqli_num_rows($result);
    if ($number) {
        $row = mysqli_fetch_assoc($result);
        $cata_id = $row['cata_id'];
        echo $cata_id;
    }

    if ($res_insert) {
    } else {
        // echo "<script>alert('系统繁忙，请稍候！');</script>";
    }
}

?>